package top.milkbox.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.milkbox.domain.Post;
import top.milkbox.domain.PostCollection;
import top.milkbox.info.R;
import top.milkbox.service.PostCollectionService;

@RestController
@RequestMapping("/postCollections")
public class PostCollectionController {

    @Autowired
    private PostCollectionService postCollectionService;

    @PostMapping("/collection.login")
    public R collection(
            @CookieValue("token") String token,
            @RequestBody Post post
    ) {
        R r = new R();
        r.setFlag(postCollectionService.collection(token, post.getId()));
        if (r.isFlag()) {
            r.setMessage("收藏成功");
        } else {
            r.setMessage("已经收藏了或者收藏出错");
        }
        return r;

    }

    @DeleteMapping("/cancelCollection.login")
    public R cancelCollection(
            @CookieValue("token") String token, Integer postId) {
        R r = new R();
        r.setFlag(postCollectionService.cancelCollection(token, postId));
        if (r.isFlag()) {
            r.setMessage("取消收藏成功");
        } else {
            r.setMessage("没有收藏或者取消收藏失败");
        }
        return r;
    }
}
